package GUI;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.LayoutManager;
import java.util.ArrayList;
import java.util.List;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.TableColumn;

import database.TableManager;
import database.TableManager.TableEnum;

public class GUI_page05_DisplayTables extends JPanel {

	JLabel titleLabel;
	JButton returnButton;
	List<String> tableOptions;
	//String[] tableOptions; = { "Item", "LeadSinger", "HasSong", "Supplier", "Store", "Shipment", "ShipItem", "Stored", "Customer", "Purchase", "PurchaseItem", "Return", "ReturnItem" };
	JComboBox tableList;
	JTable table;
	JScrollPane scrollPane;
	
	TableManager tm;
	
	public GUI_page05_DisplayTables() {
		// get number of tables and names
		tableOptions = new ArrayList<String>();
		for (TableEnum table : TableEnum.values())
            tableOptions.add(table + "");
		
		this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		this.setBackground(Color.white);
		// title label
		titleLabel = new JLabel("Admin Table Display", JLabel.CENTER);
		titleLabel.setVerticalTextPosition(JLabel.CENTER);
		
		titleLabel.setHorizontalTextPosition(JLabel.CENTER);
		titleLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
		titleLabel.setFont(new Font("Century Gothic", Font.BOLD, 20));
		titleLabel.setForeground(Color.gray);
        this.add(titleLabel);
        
        this.add(Box.createRigidArea(new Dimension(0,20)));

        // return button
        returnButton = GUI_AMSStoreMain.makeGrayWhiteButton("Back", Component.LEFT_ALIGNMENT);
        this.add(returnButton);
        
        this.add(Box.createRigidArea(new Dimension(0,15)));
        
        // list of tables to choose from, by default on first one
        Object[] tableStrings = tableOptions.toArray();
        tableList = new JComboBox(tableStrings);
        tableList.setSelectedIndex(0);
        tableList.setAlignmentX(Component.LEFT_ALIGNMENT);
        tableList.setForeground(Color.gray);
        tableList.setBackground(Color.white);
        tableList.setFont(new Font("Century Gothic", Font.PLAIN, 13));
        tableList.setMaximumSize(new Dimension(200,20));
        tableList.setSize(new Dimension(200, 20)); 
        //userList.addActionListener(this);
        this.add(tableList);
        
        this.add(Box.createRigidArea(new Dimension(0,20)));
        
        scrollPane = new JScrollPane(table);
		this.add(scrollPane);
	}

	public void populateTable(Object[] columnNames, Object[][] data){
		this.remove(scrollPane);
		
		tm = TableManager.getInstance();
		List<String> cols = tm.getTableColumnNames(TableEnum.valueOf(this.tableOptions.get(this.tableList.getSelectedIndex())));
		List<String> primaryCols = tm.getTablePrimaryKeyColumnNames(TableEnum.valueOf(this.tableOptions.get(this.tableList.getSelectedIndex())));

		for (int i = 0; i < cols.size();i++)
		{
			Boolean isPrimary = false;
			for (int j = 0; j < primaryCols.size(); j++)
			{
				if (cols.get(i).equals(primaryCols.get(j)))
				{
					isPrimary = true;
					break;
				}
			}
			if (isPrimary)
			{
				String temp = "*" + columnNames[i];
				columnNames[i] = (Object) temp;
			}
		}
		table = new JTable(data, columnNames);
		table.setPreferredScrollableViewportSize(new Dimension(500,70));
		table.setFillsViewportHeight(true);
		
		scrollPane = new JScrollPane(table);
		this.add(scrollPane);
	}
}
